package cn.ghgj.yn.jdbc_demo.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.ghgj.yn.jdbc_demo.entity.User;

public class UserDao {
	public List<User> list() throws Exception{
		List<User> lists = new ArrayList<User>();
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bd1700", "root", "root");
		Statement stmt = conn.createStatement();
		String sql = "select * from t_user";
		// 这是执行查询
		ResultSet rs = stmt.executeQuery(sql);

		while (rs.next()) {
			int id = rs.getInt("id");
			String name = rs.getString("name");
			int age = rs.getInt("age");
			User user = new User(id, name, age);
			lists.add(user);
		}

		rs.close();
		stmt.close();
		conn.close();

		return lists;
	}
}
